package com.topboya.linker.ui.presenter;

import android.util.Log;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.model.response.MiniSignInResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.HomeView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HomePresenter extends BasePresenter<HomeView> implements TencentLocationListener {

    private static final String TAG = "HomePresenter:";
    private String latitude;
    private String longitude;
    private String startTime;
    private String userid;
    JSONArray jsonArray = null;
    public HomePresenter(BaseActivity context) {
        super(context);

    }

    public void getDayByYearMoth(String startTime){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getDayByYearMoth(startTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(daybyyearmothresponse -> {
                    String success = daybyyearmothresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {
                        List<String> dayByYearMothList = daybyyearmothresponse.getData();
                    getView().getDayByYeahMonth(dayByYearMothList);

                    }

                }, this::loginError);
    }


    public void getHomeList(){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));

        LogUtils.v(TAG + startTime+"---"+longitude+"----"+latitude);
        ApiRetrofit.getInstance().getHomeList("0",userid,startTime,longitude,latitude,"30","1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( homelistresponse-> {
                    String success = homelistresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {
                        LogUtils.v(TAG + homelistresponse.toString());
                        getView().getHomeList(homelistresponse);

                    }
                }, this::loginError);
    }

//userid 是自己或者下属集合的id
    public void getLocationFromTencent(String startTime,String userid){
        LogUtils.v(TAG + "startTime:"+startTime+ "userid:"+userid);
        TencentLocationRequest request = TencentLocationRequest.create();
        request.setInterval(24*60*60*1000);
        request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_NAME);
        request.setAllowCache(true);
        TencentLocationManager locationManager = TencentLocationManager.getInstance(mContext);
        int erro = locationManager.requestLocationUpdates(request, this);
        Log.v(TAG,"tencent_map_error:"+erro);
        this.startTime = startTime;
        this.userid = userid;
    }


    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {

        LogUtils.v(TAG + "tencentLocation"+ tencentLocation.toString());
        LogUtils.v(TAG + "latitude:"+ tencentLocation.getLatitude() +"longitude:"+ tencentLocation.getLongitude());
//        String location = tencentLocation.getLatitude()+","+tencentLocation.getLongitude();
        latitude = String.valueOf(tencentLocation.getLatitude());
        longitude = String.valueOf(tencentLocation.getLongitude());
//      北京天健医院地址
//        latitude = "40.193459";
//        longitude = "116.328194";

        getHomeList();


    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {

    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }



    public void deleteTask(String id){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().deleteTask(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( deletetaskresponse-> {
                    String success = deletetaskresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {

                        getView().DeleteTaskResult();

                    }
                }, this::loginError);
    }



    public void miniSignIn(String lat, String lon, String actLat, String actLon , String operaId){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().miniSignIn(lat,lon, actLat, actLon ,operaId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( minisigninresponse-> {

                    String success = minisigninresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {

                        getView().miniSignInResult();

                    }
                }, this::loginError);
    }



}
